﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraBars;
using UniMana.Utils.Data;
using System.Linq;
using UniMana.Data;

namespace UniMana.Win.Systems
{
    public partial class FrmRuleManager : DevExpress.XtraEditors.XtraForm
    {
        DataTable m_dtRuleList;
        public FrmRuleManager()
        {
            InitializeComponent();
            InitControlEvent();
            LoadData();
        }

        public void LoadData()
        {
            m_dtRuleList = System_RuleData.PermissionRuleOfGroup().ToDataTable();
            if (m_dtRuleList.Columns.Count == 0)
            {
                m_dtRuleList.Columns.Add("System_RuleKey");
                m_dtRuleList.Columns.Add("RuleID");
                m_dtRuleList.Columns.Add("RuleName");
                m_dtRuleList.Columns.Add("RuleNameVi");
                m_dtRuleList.Columns.Add("ParentID");
                m_dtRuleList.Columns.Add("Description");
                m_dtRuleList.Columns.Add("GroupName");
                m_dtRuleList.Columns.Add("AllowAccess", System.Type.GetType("System.Boolean"));
                m_dtRuleList.Columns.Add("AllowAdd", System.Type.GetType("System.Boolean"));
                m_dtRuleList.Columns.Add("AllowDelete", System.Type.GetType("System.Boolean"));
                m_dtRuleList.Columns.Add("AllowEdit", System.Type.GetType("System.Boolean"));
                m_dtRuleList.Columns.Add("AllowExport", System.Type.GetType("System.Boolean"));
                m_dtRuleList.Columns.Add("AllowImport", System.Type.GetType("System.Boolean"));
                m_dtRuleList.Columns.Add("AllowPrint", System.Type.GetType("System.Boolean"));
                m_dtRuleList.Columns.Add("AllowSave", System.Type.GetType("System.Boolean"));
            }
            //DataTable dtTemp = System_GroupData.GetGroupHaveRule().ToDataTable();

            //foreach (DataRow dr in dtTemp.Rows)
            //{
            //    DataRow drOrgin = m_dtRuleList.NewRow();
            //    drOrgin["System_RuleKey"] = drOrgin["System_RuleKey"];
            //    drOrgin["RuleID"]=dr["RuleID"];
            //    drOrgin["RuleName"] = dr["RuleName"];
            //    drOrgin["RuleNameVi"] = dr["RuleNameVi"];
            //    drOrgin["ParentID"]=dr["ParentID"];
            //    drOrgin["Description"] =dr["Description"];
            //    drOrgin["AllowAccess"] = true;
            //    drOrgin["AllowAdd"] = true;
            //    drOrgin["AllowDelete"] = true;
            //    drOrgin["AllowEdit"] = true;
            //    drOrgin["AllowExport"] = true;
            //    drOrgin["AllowImport"] = true;
            //    drOrgin["AllowPrint"] = true;
            //    drOrgin["AllowSave"] = true;
            //    m_dtRuleList.Rows.Add(drOrgin);
            //}
            //m_dtRuleList.AcceptChanges();
            this.grcFunctionList.DataSource = m_dtRuleList;
        }

        public void DelegateRule(object[] sri)
        {
            DataRow dRow = (from DataRow dr in this.m_dtRuleList.Rows where dr["System_RuleKey"].ToString() == sri[0].ToString() select dr).FirstOrDefault();
            if (dRow != null)
            {

                dRow["System_RuleKey"] = sri[0];
                dRow["RuleID"] = sri[1];
                dRow["RuleName"] = sri[2];
                dRow["RuleNameVi"] = sri[3];
                dRow["Description"] = sri[4];
                dRow["AllowAccess"]=sri[5];
                dRow["AllowAdd"]=sri[6];
                dRow["AllowDelete"]=sri[7]; 
                dRow["AllowEdit"]=sri[8];
                dRow["AllowExport"]=sri[9];
                dRow["AllowImport"]=sri[10];
                dRow["AllowPrint"]=sri[11];
                dRow["AllowSave"] = sri[12];

                dRow["GroupName"] = sri[13];
            }
            else
            {
                DataRow dr = m_dtRuleList.NewRow();
                dr["System_RuleKey"] = sri[0];
                dr["RuleID"] = sri[1];
                dr["RuleName"] = sri[2];
                dr["RuleNameVi"] = sri[3];
                dr["Description"] = sri[4];
                dr["AllowAccess"] = sri[5];
                dr["AllowAdd"] = sri[6];
                dr["AllowDelete"] = sri[7];
                dr["AllowEdit"] = sri[8];
                dr["AllowExport"] = sri[9];
                dr["AllowImport"] = sri[10];
                dr["AllowPrint"] = sri[11];
                dr["AllowSave"] = sri[12];

                dr["GroupName"] = sri[13];
                m_dtRuleList.Rows.Add(dr);
            }
        }
        #region Khởi tạo XucBar
        public void InitControlEvent()
        {
            //xucBar1.AddNewVisible = false;
            //xucBar1.EditVisible = false;
            //xucBar1.DeleteVisible = false;
            //xucBar1.ExportExcelVisible = false;
            //xucBar1.ImportExcelVisible = false;

            //BarButtonItem btnLoad = new BarButtonItem();
            //btnLoad.Caption= "Tải dữ liệu";
            //xucBar1.Controls.Add(btnLoad);
            //xucBar1.btnSave_Click += new Controls.xucBar.ButtonClickHandler(btnSave_Click);


            xucBar1.btnAddNew_Click += new Utils.Controls.xucBar.ButtonClickHandler(btnNew_Click);
            xucBar1.btnEdit_Click += new Utils.Controls.xucBar.ButtonClickHandler(btnEdit_Click);
            xucBar1.btnDelete_Click += new Utils.Controls.xucBar.ButtonClickHandler(btnDelete_Click);
        }
        #endregion
        private void btnNew_Click(object sender, ItemClickEventArgs e)
        {
            xucBar1.dlg.Close();
            FrmCreateRule f = new FrmCreateRule();
            f.passData = new FrmCreateRule.PassData(DelegateRule);
            f.ShowDialog();
        }

        private void btnEdit_Click(object sender, ItemClickEventArgs e)
        {
            xucBar1.dlg.Close();
            try
            {
                string strRuleID = this.grvFunctionList.GetFocusedRowCellValue("RuleID").ToString();
                System_RuleInfo sri = new System_RuleInfo(strRuleID);
                FrmCreateRule f = new FrmCreateRule(sri);
                f.passData = new FrmCreateRule.PassData(DelegateRule);
                f.ShowDialog();
            }
            catch { }
            
        }
        private void btnDelete_Click(object sender, ItemClickEventArgs e)
        {
            try
            {
                DialogResult dgr = MessageBox.Show("Bạn muốn xóa luật phân quyền", "Thông báo", MessageBoxButtons.OKCancel);
                if (dgr == System.Windows.Forms.DialogResult.OK)
                {
                    string strRuleID = this.grvFunctionList.GetFocusedRowCellValue("RuleID").ToString();
                    System_RuleInfo sri = new System_RuleInfo(strRuleID);
                    if (sri.Properties.ParentID != "")
                    {
                        System_Group_RuleInfo sgri = new System_Group_RuleInfo(int.Parse(sri.Properties.ParentID), sri.Properties.System_RuleKey);

                        DataTable dtGr = System_UserData.GetUserByGroup(sgri.Properties.System_GroupKey).ToDataTable();
                        foreach (DataRow dr in dtGr.Rows)
                        {
                            System_User_RuleInfo suri = new System_User_RuleInfo(Guid.Parse(dr["System_UserKey"].ToString()), sri.Properties.System_RuleKey);
                            suri.Delete();
                            suri.Save();
                        }
                        sgri.Delete();
                        sgri.Save();
                    }
                    sri.Delete();
                    sri.Save();

                    this.grvFunctionList.DeleteSelectedRows();
                }
            }
            catch { }
        }
    }
}